﻿using MiniJob.Orleans;
using MiniJob.Service;

namespace MiniJob;

public class InitService : IHostedService
{
    private readonly IServiceScope _localServiceScope;
    private readonly IDataSeedService dataSeedService;
    private readonly IGrainFactory grainFactory;

    public InitService(IServiceScopeFactory serviceScopeFactory)
    {
        _localServiceScope = serviceScopeFactory.CreateScope();
        dataSeedService = _localServiceScope.ServiceProvider.GetRequiredService<IDataSeedService>();
        grainFactory = _localServiceScope.ServiceProvider.GetRequiredService<IGrainFactory>();
    }

    public Task StartAsync(CancellationToken cancellationToken)
    {
        _ = dataSeedService.SeedAsync(cancellationToken);
        _ = grainFactory.GetGrain<IScheduleGrain>(0).StartAsync();
        if (Appsettings.IsAdminConsoleMode)
        {

        }
        return Task.CompletedTask;
    }

    public Task StopAsync(CancellationToken cancellationToken)
    {
        _localServiceScope?.Dispose();
        return Task.CompletedTask;
    }
}
